package com.coocaa.ops.common.core.base.model;


import com.coocaa.ops.common.tool.http.HttpCodeEnums;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tangmimghong
 * @date : 2020.6.11
 * @link {http://wiki.skyoss.com/pages/viewpage.action?pageId=24421939}
 */
public class R extends HashMap<String, Object> {

    public static final String SUCCESS = "success";

    public static final String ERR_MSG = "The system is lost, please try again later.";

    public R() {
        put("code", "0");
        put("success", true);
        put("msg", "success");
    }

    public R msg(String msg) {
        this.put("msg", msg);
        return this;
    }

    public R data(Object data) {
        this.put("data", data);
        return this;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.put("data", map);
        return r;
    }

    /**
     * 成功的-获取不分页的数据结构
     *
     * @param list
     * @return
     */
    public static R ok(Collection<?> list) {
        R r = new R();
        r.put("data", list);
        return r;
    }

    /**
     * 成功的-列表分页的返回数据结构
     *
     * @param total
     * @param rows
     * @return
     */
    public static R ok(int total, Collection<?> rows) {
        R r = new R();
        Map<String, Object> data = new HashMap<>(2);
        data.put("total", total);
        data.put("rows", rows);
        r.put("data", data);
        return r;
    }

    /**
     * 成功的-列表分页的返回数据结构
     *
     * @param total
     * @param rows
     * @return
     */
    public static R ok(int total, Collection<?> rows, Object extra) {
        R r = new R();
        Map<String, Object> data = new HashMap<>(2);
        data.put("total", total);
        data.put("rows", rows);
        data.put("extra", extra);
        r.put("data", data);
        return r;
    }

    /**
     * 成功的-列表分页的返回数据结构
     *
     * @param pageResultModel
     * @return
     */
    public static R ok(PageResultModel<?> pageResultModel) {
        return ok(pageResultModel.getTotal(), pageResultModel.getResultList());
    }

    /**
     * 成功的-列表分页的返回数据结构
     *
     * @param pageResultModel
     * @return
     */
    public static R ok(PageResultModel<?> pageResultModel, Object extra) {
        return ok(pageResultModel.getTotal(), pageResultModel.getResultList(), extra);
    }

    /**
     * 成功的-新增等
     *
     * @return
     */
    public static R ok() {
        return new R();
    }


    public static R fail(String msg) {
        return error("-1001", msg);
    }

    public static R fail(String msg, String detailMsg) {
        R result = error("-1001", msg);
        result.put("detailMsg", detailMsg);
        return result;
    }

    public static R error() {
        return error(HttpCodeEnums.INTERNAL_SERVER_ERROR.value().toString(), ERR_MSG);
    }

    public static R error(Map data) {
        R result = error(HttpCodeEnums.INTERNAL_SERVER_ERROR.value().toString(), ERR_MSG);
        result.data(data);
        return result;
    }


    public static R error(String code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        r.put("success", false);
        return r;
    }

    public static boolean isResultOK(R result) {
        return "0".equals(result.get("code"));
    }

}
